package com.lxl.mayi;

import org.apache.hadoop.mapreduce.Reducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @Author lixilin
 * @Date 2022/1/1 11:18
 * @Description
 */
public class UserCarbonCountCombiner extends Reducer<UserCarbon, UserCarbon, UserCarbon, UserCarbon> {
    private static final Logger LOG = LoggerFactory.getLogger(UserCarbonCountCombiner.class);
    /**
     * Combiner 在map执行完成,进reducer 之前对map的结果进行
     * 求出当天carbon大于等于100 的数据
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(UserCarbon key, Iterable<UserCarbon> values, Context context) throws IOException, InterruptedException {
        int carbonCount = 0;
        for (UserCarbon value : values) {
            carbonCount+=value.getCarbon();
        }
        if (carbonCount >= 100) {
            key.setCarbon(carbonCount);
            context.write(key,key);
        }
    }
}
